package com.iyao_app.adapter;

import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.R;
import com.iyao_app.entry.DemoData;
import com.iyao_app.entry.MyInterestBean;
import com.iyao_app.mvp.sign.SignActivity;
import com.iyaowoqu.base_library.util.LogUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.github.rockerhieu.emojicon.EmojiconTextView;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/23.
 */

public class NotifyFriendAdapter extends BaseRecyclerAdapter<MyInterestBean, NotifyFriendAdapter.ViewHolder> {
    SignActivity signActivity;

    public NotifyFriendAdapter(SignActivity context, List<MyInterestBean> data) {
        super(context, data);
        signActivity = context;
    }

    @Override
    protected ViewHolder getViewHolder(int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.item_notify_friend, null));
    }

    @Override
    protected void onBindViewHolder_(final ViewHolder holder, int position) {
        final MyInterestBean demoData = data.get(position);
        try {
            Uri uri = Uri.parse(demoData.getFavicon());
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setResizeOptions(new ResizeOptions(100, 100))
                    .build();
            PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                    .setOldController(holder.mSdvPhoto.getController())
                    .setImageRequest(request)
                    .build();
            holder.mSdvPhoto.setController(controller);
        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.mIvSelect.setSelected(demoData.isSelect());
        holder.mTvName.setText(demoData.getNickName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.e("=====");
                if (holder.mIvSelect.isSelected()) {
                    holder.mIvSelect.setSelected(false);
                    signActivity.deleteData(demoData);
                } else {
                    holder.mIvSelect.setSelected(true);
                    signActivity.addData(demoData);
                }
                demoData.setSelect(holder.mIvSelect.isSelected());
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.sdv_photo)
        SimpleDraweeView mSdvPhoto;
        @BindView(R.id.iv_select)
        ImageView mIvSelect;
        @BindView(R.id.tv_name)
        EmojiconTextView mTvName;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
